local assets =
{
    -- Asset("ANIM", "anim/panda_fisherman_supply_pack.zip"),
    -- Asset( "IMAGE", "images/inventoryimages/panda_fisherman_supply_pack.tex" ),  -- 背包贴图
    -- Asset( "ATLAS", "images/inventoryimages/panda_fisherman_supply_pack.xml" ),
}


local function fn()

    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddNetwork()

    -- inst:AddTag("CLASSIFIED")
    -- inst:AddTag("NOBLOCK")
    -- inst:AddTag("NOCLICK")
    -- inst:AddTag("FX")
    MakeInventoryPhysics(inst)
    MakeHauntableLaunch(inst)
    inst.entity:SetPristine()

    inst.AnimState:SetBank("gift")
    inst.AnimState:SetBuild("gift")
    inst.AnimState:PlayAnimation("idle_large2")
    local gift_anim = {
        "idle_large1",
        "idle_large2",
        "idle_medium1",
        "idle_medium2",
        "idle_small1",
        "idle_small2",
    }


    inst:AddTag("gift")
    inst:AddTag("npc_gift_pack")

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("named")
    inst:AddComponent("inspectable") --可检查组件
    inst:AddComponent("npc_everything_data")
    inst:AddComponent("npc_base_lib")

    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem:SetSinks(true)
    inst.components.inventoryitem:ChangeImageName("gift_large2")
    -- inst.components.inventoryitem.imagename = "panda_fisherman_supply_pack"
    -- inst.components.inventoryitem.atlasname = "images/inventoryimages/panda_fisherman_supply_pack.xml"
	inst.components.inventoryitem.cangoincontainer = true

    inst.Set_Skin = function(self,num)
        local Skin_table = {
            [1] = {"idle_small1","gift_small1"},
            [2] = {"idle_small2","gift_small2"},
            [3] = {"idle_medium1","gift_medium1"},
            [4] = {"idle_medium2","gift_medium2"},
            [5] = {"idle_large1","gift_large1"},
            [6] = {"idle_large2","gift_large2"},
        }

        local ret_skin_table = Skin_table[num] or Skin_table[math.random(#Skin_table)]
        self.AnimState:PlayAnimation(ret_skin_table[1])
        self.components.inventoryitem:ChangeImageName(ret_skin_table[2])

    end

    --------------------------------------------------------------------------------------
    inst:AddComponent('container')
    inst.components.container.canbeopened = false
    inst.components.container.numslots = 20
    
    inst:ListenForEvent("itemget",function(inst,_table)
        if _table and _table.item then
            local tempItem = _table.item
            if tempItem.components.perishable then
                tempItem.components.perishable:StopPerishing()
            end
        end
    end)

    inst:ListenForEvent("itemlose",function(inst,_table)
        if _table and _table.prev_item then
            local tempItem = _table.prev_item
            if tempItem.components.perishable then
                tempItem.components.perishable:StartPerishing()
                tempItem.components.perishable:SetPercent(1)
            end
        end
    end)
    --------------------------------------------------------------------------------------

    local function OnUnwrapped(inst, pos, doer)
        inst.components.container:DropEverything()
        inst:Remove()
    end
    inst:AddComponent("unwrappable")
    -- inst.components.unwrappable:SetOnWrappedFn(OnWrapped)
    inst.components.unwrappable:SetOnUnwrappedFn(OnUnwrapped)
    -----------------------------------------------------------------------------------
    -- 用 Event("ADD",_table) 添加物品到包里。 格式： num 如果 小于1，则是概率，random < num 的时候放置1个
    -- _table = {
    --      Items = {
    --                {"log",num，fn},
    --                {"goldnugget",fn}
    --      }
    --      Name = "pack name"
    --      Inspect_str = "66666666"
    --      Skin = 1,   -- 1~6
    -- }
    ---------------------------------------
    local function Add_items(inst,_table)
        if _table == nil then
            return
        end

        if _table.Items ~= nil then
            for k, theCMD in pairs(_table.Items) do
                local itemName = theCMD[1]
                local itemNum = theCMD[2]
                local item_fn = theCMD[3]
                if itemName == nil then
                    return
                end
                if itemNum == nil then
                    itemNum = 1
                end
    
    
                if itemNum < 1 then
                   local tempNum = math.random(100)/100
                   if tempNum < itemNum then
                        itemNum = 1
                   end
                end
    
                if itemNum >=1 then
                    -- for i = 1, itemNum, 1 do
                    --     inst.components.container:GiveItem(SpawnPrefab(itemName))
                    -- end
                    inst.npc_base_lib:GiveItemByName(itemName,itemNum)
                    if item_fn and type(item_fn) == "function" then
                        inst.components.container:ForEachItem(function(item_inst)
                            if item_inst and item_inst.prefab == itemName then
                                pcall(item_fn,item_inst)
                            end
                        end)
                    end
                end
                
            end
        end



        if _table.Name then
            inst.components.named:SetName(_table.Name)
        else
            inst.components.named:SetName("Gift")
        end

        if _table.Inspect_str then
            inst.components.inspectable:SetDescription(_table.Inspect_str)
        else
            inst.components.inspectable:SetDescription("Gift")
        end

        if _table.Skin then
            inst:Set_Skin(_table.Skin)
        end

        _table.Items = nil
        inst.components.npc_everything_data:Set("CMD",_table)
    end

    inst:ListenForEvent("ADD",Add_items)  
    inst:ListenForEvent("Set",Add_items)  

    inst:DoTaskInTime(0,function()
        local _table = inst.components.npc_everything_data:Get("CMD")
        if _table == nil then
            return
        end
        if _table then
            inst:PushEvent("Set",_table)
        end
    end)
   
    return inst
end

return Prefab("npc_gift_pack", fn,assets)